//package com.smile.config;
//
//import cn.dev33.satoken.stp.StpUtil;
//import cn.hutool.core.util.ObjectUtil;
//import feign.RequestInterceptor;
//import feign.RequestTemplate;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.nio.charset.Charset;
//import java.util.Enumeration;
//
///**
// * Feign 调用的时候传token到下游
// */
//public class FeignRequestInterceptor implements RequestInterceptor {
//    @Override
//    public void apply(RequestTemplate template) {
//        String url = template.feignTarget().url();
//        System.out.println("url = " + url);
//        String name1 = template.feignTarget().name();
//        System.out.println("name =" + name1);
//        String path = template.path();
//        System.out.println("path = " + path);
//        if (ObjectUtil.isNotNull(template.body())){
//
//            byte[] body = template.body();
//            System.out.println(new String(body, Charset.defaultCharset()));
//        }
//
////        template.uri("/user/getUserInfoByUserName");
//        // 从header获取X-token
////        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
////        ServletRequestAttributes attr = (ServletRequestAttributes) requestAttributes;
////        HttpServletRequest request = attr.getRequest();
////        Collection<String> requestVariables = template.getRequestVariables();
////        System.out.println(requestVariables);
////        System.out.println("url = " + request.getRequestURL());
////        System.out.println("uri = " + request.getRequestURI());
////        Enumeration<String> headerNames = request.getHeaderNames();
////        while (headerNames.hasMoreElements()) {
////            String headerName = headerNames.nextElement();
////            String headerValue = request.getHeader(headerName);
////            System.out.println(headerName + ":" + headerValue);
////            if ("cookie".equals(headerName)) {
////                System.out.println(headerName + ":" + headerValue);
////                String[] split = headerValue.split("=");
////                template.header(split[0], split[1]);
////                System.out.println("tokenName" + StpUtil.getTokenName());
////                template.header(headerName, "Bearer " +split[1]);
////                template.header("Authorization", "Bearer " +split[1]);
////                template.header("satoken", "Bearer " +split[1]);
////
////            }
////            template.header(headerName, headerValue);
//
//
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
//                .getRequestAttributes();
//        if (attributes == null) {
//            return;
//        }
//        HttpServletRequest request = attributes.getRequest();
//        Enumeration<String> headerNames = request.getHeaderNames();
//        if (headerNames != null) {
//            while (headerNames.hasMoreElements()) {
//                String name = headerNames.nextElement();
//                String values = request.getHeader(name);
//
//                if (name.equals("content-length")){
//                    continue;
//                }
//
//                if ("cookie".equals(name)) {
//                    System.out.println(name + ":" + values);
//                    String[] split = values.split("=");
//                    template.header(split[0], split[1]);
//                    System.out.println("tokenName" + StpUtil.getTokenName());
//                    template.header(name,  split[1]);
//                    template.header("Authorization",  split[1]);
//                    template.header("satoken", split[1]);
//
//                    //跳过 content-length，避免追加参数导致content-length值与实际长度不一样
//                    if (name.equals("content-length")) {
//                        continue;
//                    }
//                    template.header(name, values);
//                }
//            }
//
//            template.header("link-URL", "client");
//
//        }
//
////        String token = request.getHeader("x-auth-token");//网关传过来的 token
////        if (StringUtils.hasText(token)) {
////            template.header("X-AUTH-TOKEN", token);
////        }
//    }
//}
//
